package com.gexiaobao.pigeon.ui.adapter

import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.EditBloodInfoBean
import com.gexiaobao.pigeon.databinding.ItemElderBinding

/**
 * @Author      : hxw
 * @Date        : 2024/9/14 11:22
 * @Describe    :
 */
class AdapterOneGeneration(data: ArrayList<EditBloodInfoBean>) :
    BaseQuickAdapter<EditBloodInfoBean, DataBindBaseViewHolder>(R.layout.item_elder, data) {

    private lateinit var llParent: LinearLayout

    override fun convert(holder: DataBindBaseViewHolder, item: EditBloodInfoBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemElderBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        llParent = holder.getView(R.id.llParentLayout)
        llParent.isFocusable = item.isClickable
        llParent.isClickable = item.isClickable

        holder.setGone(R.id.ivItemAddData, item.ringId.isNotEmpty())

        val ringIdBg: Int
        val sexIcon: Int
        val ringIdTvColor: Int
        when (holder.layoutPosition) {
            0 -> {
                ringIdBg = R.drawable.round8_male_pigeon_linear_bg
                sexIcon = R.mipmap.icon_male
                ringIdTvColor = R.color.comm_text_gray_dark
            }

            1 -> {
                ringIdTvColor = R.color.white
                ringIdBg = R.drawable.round8_red_half_top
                sexIcon = when (item.sex) {
                    "雌" -> {
                        R.mipmap.icon_female
                    }

                    "雄" -> {
                        R.mipmap.icon_male
                    }

                    else -> {
                        R.mipmap.icon_sex_unknow
                    }
                }
            }

            else -> {
                ringIdTvColor = R.color.comm_text_gray_dark
                sexIcon = R.mipmap.icon_female
                ringIdBg = R.drawable.round8_female_pigeon_linear_bg
            }
        }
        holder.setBackgroundResource(R.id.tvItemRingId, ringIdBg)
        holder.setImageResource(R.id.ivItemSex, sexIcon)
        holder.setTextColor(R.id.tvItemRingId, ContextCompat.getColor(context, ringIdTvColor))

        // 动态设置边距
        val layoutParams = holder.itemView.layoutParams as ViewGroup.MarginLayoutParams
        if (holder.layoutPosition == 1) {
            layoutParams.setMargins(2, 0, 0, 0)
        } else {
            layoutParams.setMargins(24, 0, 0, 0)
        }
        holder.itemView.layoutParams = layoutParams

        val params: ViewGroup.LayoutParams = holder.getView<AppCompatTextView>(R.id.tvItemRingId).layoutParams
        if (holder.layoutPosition == 1) {
            params.height = 95 // 根据位置动态设置高度
        } else {
            params.height = 65
        }
        holder.getView<AppCompatTextView>(R.id.tvItemRingId).layoutParams = params
    }

    // 设置是否可点击
    fun canClick(position: Int, isClick: Boolean) {
        if (position in data.indices) {
            data[position].setIsClick(isClick)
            notifyItemChanged(position)
        }
    }

    fun updateItemShow(position: Int, shouldHide: Boolean) {
        if (position in data.indices) {
            data[position] = data[position].copy(isVisible = shouldHide)
            notifyItemChanged(position) // 对于 RecyclerView
        }
    }

    fun setClickFalse(clickAble: Boolean) {
        llParent.isClickable = clickAble
        llParent.isEnabled = clickAble
        llParent.isFocusable = clickAble
    }


    // 清空某一条数据
    fun clearItem(position: Int) {
        if (position in data.indices) {
            data[position].clear() // 清空对象的所有值
            notifyItemChanged(position) // 通知更新该项
        }
    }

    // 更新单个 item 的方法
    fun updateItem(position: Int, dataBean: EditBloodInfoBean?) {
        if (position >= 0 && position < data.size) {
            val item: EditBloodInfoBean = data[position]
            if (dataBean != null) {
                item.publicIs = dataBean.publicIs
                item.country = dataBean.country
                item.countryEn = dataBean.countryEn
                item.featherColor = dataBean.featherColor
                item.ringId = dataBean.ringId
                item.name = dataBean.name
                item.sex = dataBean.sex
                item.eye = dataBean.eye
                item.gradesDesc = dataBean.gradesDesc
                item.title = dataBean.title
                item.level = dataBean.level
                item.levelInfo = dataBean.levelInfo
            }
            notifyItemChanged(position)
        }
    }
}

